using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace Acms.SqlDal
{
    public class EmailDal : Base
    {
        public enum Fields
        {
            Id,
            UserId,
            FilePath,
            To,
            Subject,
            Body,
            Bcc,
            CC,
            From,
            ReplyTo,
            Html,
            Sent,
            Created
        }

        public EmailDal() : base() { }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.UserId, reader, table);
            GetReaderValue<string>(Fields.FilePath, reader, table);
            GetReaderValue<string>(Fields.To, reader, table);
            GetReaderValue<string>(Fields.CC, reader, table);
            GetReaderValue<string>(Fields.Bcc, reader, table);
            GetReaderValue<string>(Fields.From, reader, table);
            GetReaderValue<string>(Fields.ReplyTo, reader, table);
            GetReaderValue<string>(Fields.Subject, reader, table);
            GetReaderValue<string>(Fields.Body, reader, table);
            GetReaderValue<bool>(Fields.Html, reader, table);
            GetReaderValue<DateTime>(Fields.Sent, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
        }

        public void Update(ref int id, int userId, string filePath, string to, string subject, string body, string bcc, string cc, string from, string replyTo, bool html, DateTime created, DateTime sent, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "FilePath", filePath, SqlDbType.VarChar, 200);
            AddInputParameter(cmd, "To", to, SqlDbType.VarChar, 200);
            AddInputParameter(cmd, "Subject", subject, SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "Body", body, SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "Bcc", bcc, SqlDbType.VarChar, 200);
            AddInputParameter(cmd, "CC", cc, SqlDbType.VarChar, 200);
            AddInputParameter(cmd, "From", from, SqlDbType.VarChar, 200);
            AddInputParameter(cmd, "ReplyTo", replyTo, SqlDbType.VarChar, 200);
            AddInputParameter(cmd, "Html", html, SqlDbType.Bit);
            AddInputParameter(cmd, "Created", created, SqlDbType.DateTime);
            AddInputParameter(cmd, "Sent", sent, SqlDbType.DateTime);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);
            ExecuteCommand(cmd);
            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }
    }
}
